package com.orange.off.cloud.cloudoff.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.orange.off.cloud.cloudoff.R;
import com.orange.off.cloud.cloudoff.app.MyApplication;
import com.orange.off.cloud.cloudoff.events.PersonalUpdateInformationEvent;
import com.orange.off.cloud.cloudoff.events.PersonalUpdateNameEvent;
import com.orange.off.cloud.cloudoff.model.UploadResponse;
import com.orange.off.cloud.cloudoff.urls.AppUrls;
import com.orange.off.cloud.cloudoff.utils.ProgressDlgUtil;
import com.orange.off.cloud.cloudoff.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

/**
 * 个人中心------我的资料------修改姓名
 *
 * @author qs
 */
public class PersonalInformationChangeNameFragment extends Fragment {


    @Bind(R.id.rt_profileBack)
    RelativeLayout rtProfileBack;
    @Bind(R.id.tvBtn_save)
    TextView tvBtnSave;
    @Bind(R.id.edt_changeName)
    EditText edtChangeName;

    public static PersonalInformationChangeNameFragment newInstance(String name) {
        PersonalInformationChangeNameFragment fragment = new PersonalInformationChangeNameFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.changenamefragment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    private void initViews() {
        edtChangeName.setText(getArguments().getString("name"));
    }


    @OnClick({R.id.rt_profileBack, R.id.tvBtn_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rt_profileBack:
                //返回：
                getActivity().finish();
                break;
            case R.id.tvBtn_save:
                //保存提交：
                ProgressDlgUtil.showProgressDlg("数据提交中,请稍后...", getActivity());
                RequestParams params = new RequestParams();//请求参数
                params.addFormDataPart("aid", MyApplication.aid);
                params.addFormDataPart("image", "");
                params.addFormDataPart("name", edtChangeName.getText().toString().trim());
                HttpRequest.post(AppUrls.UpdateInformation, params, new BaseHttpRequestCallback<UploadResponse>() {
                    @Override
                    public void onSuccess(UploadResponse uploadResponse) {
                        super.onSuccess(uploadResponse);
                        ProgressDlgUtil.stopProgressDlg();
                        Log.d(MyApplication.DEBUG, "personal-nameupdate-success-》" + uploadResponse.toString());
                        ToastUtils.show(getActivity(), uploadResponse.getMessage());
                        if (uploadResponse.getSuccess()) {
                            EventBus.getDefault().post(new PersonalUpdateInformationEvent(true));
                            EventBus.getDefault().post(new PersonalUpdateNameEvent(edtChangeName.getText().toString().trim()));
                            getActivity().finish();
                        } else {
                            ToastUtils.show(getActivity(), uploadResponse.getMessage(), Toast.LENGTH_SHORT);
                        }

                    }

                    @Override
                    public void onFailure(int errorCode, String msg) {
                        super.onFailure(errorCode, msg);
                        ProgressDlgUtil.stopProgressDlg();
                    }

                    @Override
                    public void onProgress(int progress, long networkSpeed, boolean done) {
                    }
                });
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            getActivity().finish();
            return true;
        }
        return false;
    }
}
